#!/bin/bash

###############################################################################
# JWMWindowConfig v0.5 by SFR'2014                                            #
# GNU GPL v2 applies                                                          #
# Partially based on "title-clock" and "focusModel" scripts                   #
# from "jwmconfig2" package                                                   #
###############################################################################

export TEXTDOMAIN=jwm
export OUTPUT_CHARSET=UTF-8

APPDIR="`dirname $0`"
[ "$APPDIR" = "." ] && APPDIR="`pwd`"
export APPDIR="$APPDIR"
export WORKDIR=$HOME/.jwm/tmp
[ ! -d "$WORKDIR" ] && mkdir -p "$WORKDIR"

[ ! "`ps -A | grep jwm`" ] && /usr/lib/gtkdialog/box_ok JWM info "$(gettext 'You are currently NOT running the JWM window manager, and settings here will not work until JWM is active.')"

export CONFIG_FILE="$HOME/.jwm/jwmrc-personal"

#------------------------------------------------------------------------------

defTITLE_HEIGHT="`grep -FA 100 '<WindowStyle>' "$HOME/.jwm/jwmrc-theme" | grep -m1 '<Height>' | cut -f2 -d '>' | cut -f1 -d '<' | tr -cd [0-9]`"
[ "$defTITLE_HEIGHT" = "" ] && defTITLE_HEIGHT=22
defCORNER_RADIUS="`grep -FA 100 '<WindowStyle>' "$HOME/.jwm/jwmrc-theme" | grep -m1 '<Corner>' | cut -f2 -d '>' | cut -f1 -d '<' | tr -cd [0-9]`"
[ "$defCORNER_RADIUS" = "" ] && defCORNER_RADIUS=4
defBORDER_WIDTH="`grep -FA 100 '<WindowStyle>' "$HOME/.jwm/jwmrc-theme" | grep -m1 '<Width>' | cut -f2 -d '>' | cut -f1 -d '<' | tr -cd [0-9]`"
[ "$defBORDER_WIDTH" = "" ] && defBORDER_WIDTH=4

#snap
SNAP_MODE="`grep -F '<SnapMode' "$CONFIG_FILE" | cut -f2 -d '>' | cut -f1 -d '<'`"
defSNAP_NONE=true; defSNAP_SCREEN=false; defSNAP_BORDER=false
[ "$SNAP_MODE" = "screen" ] && { defSNAP_NONE=false; defSNAP_SCREEN=true; }
[ "$SNAP_MODE" = "border" ] && { defSNAP_NONE=false; defSNAP_BORDER=true; }
#focus
FOCUS_MODEL="`grep -F '<FocusModel>' "$CONFIG_FILE" | cut -f2 -d '>' | cut -f1 -d '<'`"
defFOCUS_CLICK=true; defFOCUS_SLOPPY=false
[ "$FOCUS_MODEL" = "sloppy" ] && { defFOCUS_SLOPPY=true; defFOCUS_CLICK=false; }
#move
MOVE_MODE="`grep -F '<MoveMode' "$CONFIG_FILE" | cut -f2 -d '>' | cut -f1 -d '<'`"
defMOVE_OUTLINE=true; defMOVE_OPAQUE=false
[ "$MOVE_MODE" = "opaque" ] && { defMOVE_OUTLINE=false; defMOVE_OPAQUE=true; }
#resize
RESIZE_MODE="`grep -F '<ResizeMode' "$CONFIG_FILE" | cut -f2 -d '>' | cut -f1 -d '<'`"
defRESIZE_OUTLINE=true; defRESIZE_OPAQUE=false
[ "$RESIZE_MODE" = "opaque" ] && { defRESIZE_OUTLINE=false; defRESIZE_OPAQUE=true; }

#delay - move window to neighbor desktop
MOVE_DELAY="`grep -F '<MoveMode' "$CONFIG_FILE" | grep -F ' delay="' | awk -F 'delay="' '{print $2}' | cut -d'"' -f1`"
[[ ! "$MOVE_DELAY" || "$MOVE_DELAY" = '0' ]] && defMOVE_DELAY=false || defMOVE_DELAY=true
#aerosnap
AEROSNAP="`grep -F '>aerosnap<' "$CONFIG_FILE"`"
[[ "`grep -F '<!--' <<< "$AEROSNAP"`" ]] && defAEROSNAP=false || defAEROSNAP=true
#tiled
TILED="`grep -F '>tiled<' "$CONFIG_FILE"`"
[[ "`grep -F '<!--' <<< "$TILED"`" ]] && defTILED=false || defTILED=true
#noturgent
NOTURGENT="`grep -F '>noturgent<' "$CONFIG_FILE"`"
[[ "`grep -F '<!--' <<< "$NOTURGENT"`" ]] && defNOTURGENT=true || defNOTURGENT=false

export FOCUS_MODEL SNAP_MODE MOVE_MODE RESIZE_MODE AEROSNAP TILED NOTURGENT MOVE_DELAY
export defTITLE_HEIGHT defFOCUS_CLICK defFOCUS_SLOPPY defSNAP_NONE defSNAP_SCREEN defSNAP_BORDER defMOVE_OUTLINE defMOVE_OPAQUE defRESIZE_OUTLINE defRESIZE_OPAQUE defCORNER_RADIUS defBORDER_WIDTH

func_save_settings () {
	#height is a part of jwmrc-theme - not jwmrc-personal as other options here
	#there is potential many <Height>, and we must ensure to only change <WindowStyle>
	grep -iFA 100 '<WindowStyle>' $HOME/.jwm/jwmrc-theme > $WORKDIR/window_tmp
	grep -iFB 100 '</WindowStyle>' $WORKDIR/window_tmp | grep -vE "<Height>|<Width>|<Corner>|<WindowStyle>" > $WORKDIR/window_windowstyle
	#merge with jwmrc-theme
	grep -iFB 1000 '<WindowStyle>' $HOME/.jwm/jwmrc-theme | grep -vF '<WindowStyle>' > $WORKDIR/window_tmp
	grep -iFA 1000 '</WindowStyle>' $HOME/.jwm/jwmrc-theme | grep -vF '</WindowStyle>' > $WORKDIR/window_tmp1
	cat $WORKDIR/window_tmp					 > $HOME/.jwm/jwmrc-theme
	echo "<WindowStyle>
	<Height>${varTITLE_HEIGHT}</Height>
	<Width>${varBORDER_WIDTH}</Width>
	<Corner>${varCORNER_RADIUS}</Corner>"	>> $HOME/.jwm/jwmrc-theme
	cat $WORKDIR/window_windowstyle 		>> $HOME/.jwm/jwmrc-theme
	cat $WORKDIR/window_tmp1 				>> $HOME/.jwm/jwmrc-theme

	#snap
	VAL=none
	[ "$varSNAP_SCREEN" = "true" ] && VAL="screen"
	[ "$varSNAP_BORDER" = "true" ] && VAL="border"
	VAL_OLD="${SNAP_MODE}</SnapMode>"
	VAL_NEW="${VAL}</SnapMode>"
	[ "${VAL_NEW}" != "${VAL_OLD}" ] && sed -i 's%'${VAL_OLD}'%'${VAL_NEW}'%' "$CONFIG_FILE"
	#focus
	[ "$varFOCUS_SLOPPY" = "true" ] && VAL="sloppy" || VAL="click"
	VAL_OLD="${FOCUS_MODEL}</FocusModel>"
	VAL_NEW="${VAL}</FocusModel>"
	[ "${VAL_NEW}" != "${VAL_OLD}" ] && sed -i 's%'${VAL_OLD}'%'${VAL_NEW}'%' "$CONFIG_FILE"
	#move
	[ "$varMOVE_OUTLINE" = "true" ] && VAL=outline || VAL=opaque
	VAL_OLD="${MOVE_MODE}</MoveMode>"
	VAL_NEW="${VAL}</MoveMode>"
	[ "${VAL_NEW}" != "${VAL_OLD}" ] && sed -i 's%'${VAL_OLD}'%'${VAL_NEW}'%' "$CONFIG_FILE"
	#resize
	[ "$varRESIZE_OUTLINE" = "true" ] && VAL=outline || VAL=opaque
	VAL_OLD="${RESIZE_MODE}</ResizeMode>"
	VAL_NEW="${VAL}</ResizeMode>"
	[ "${VAL_NEW}" != "${VAL_OLD}" ] && sed -i 's%'${VAL_OLD}'%'${VAL_NEW}'%' "$CONFIG_FILE"

	#delay - move window to neighbor desktop
	[ "$varMOVE_DELAY" = "true" ] && VAL=1000 || VAL=0
	VAL_OLD=" delay=\"${MOVE_DELAY}\""
	VAL_NEW=" delay=\"${VAL}\""
	[ "${VAL_NEW}" != "${VAL_OLD}" ] && sed -i "s%${VAL_OLD}%${VAL_NEW}%" "$CONFIG_FILE"
	#aerosnap
	[ "$varAEROSNAP" = "true" ] && VAL='<Option>aerosnap</Option>' || VAL='<!-- <Option>aerosnap</Option> -->'
	[ "${AEROSNAP}" != "${VAL}" ] && sed -i "s%${AEROSNAP}%${VAL}%" "$CONFIG_FILE"
	#tiled
	[ "$varTILED" = "true" ] && VAL='<Option>tiled</Option>' || VAL='<!-- <Option>tiled</Option> -->'
	[ "${TILED}" != "${VAL}" ] && sed -i "s%${TILED}%${VAL}%" "$CONFIG_FILE"
	#noturgent
	[ "$varNOTURGENT" = "true" ] && VAL='<!-- <Option>noturgent</Option> -->' || VAL='<Option>noturgent</Option>'
	[ "${NOTURGENT}" != "${VAL}" ] && sed -i "s%${NOTURGENT}%${VAL}%" "$CONFIG_FILE"

	/usr/local/jwm_config/jwm_restart
}
export -f func_save_settings
#------------------------------------------------------------------------------

export GUI_MAIN='
<window title="'$(gettext "Window Settings")'" image-name="/usr/share/pixmaps/puppy/windows.svg">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed windows.svg 60 "$(gettext '<b>Window settings</b>')" "$(gettext 'You are currently running the JWM window manager')"`"'        
  <vbox space-expand="true" space-fill="true">

    <frame '$(gettext "Window Settings")'>
      <hbox tooltip-text="'$(gettext 'Heigth of title bar in pixels')'" space-expand="true" space-fill="true">
        <text use-markup="true" space-expand="false" space-fill="false"><label>"<b>'$(gettext "Title bar height")'</b>"</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <spinbutton range-min="2" range-max="256" range-step="1" space-expand="false" space-fill="false">
          <variable>varTITLE_HEIGHT</variable>
          <default>'${defTITLE_HEIGHT}'</default>
        </spinbutton>
      </hbox>
      <hbox tooltip-text="'$(gettext 'The window border width in pixels')'" space-expand="true" space-fill="true">
        <text use-markup="true" space-expand="false" space-fill="false"><label>"<b>'$(gettext "Border width")'</b>"</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <spinbutton range-min="1" range-max="128" range-step="1" space-expand="false" space-fill="false">
          <variable>varBORDER_WIDTH</variable>
          <default>'${defBORDER_WIDTH}'</default>
        </spinbutton>
      </hbox>
      <hbox tooltip-text="'$(gettext 'Radius of window corners in pixels')'" space-expand="true" space-fill="true">
        <text use-markup="true" space-expand="false" space-fill="false"><label>"<b>'$(gettext "Corner radius")'</b>"</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <spinbutton range-min="0" range-max="5" range-step="1" space-expand="false" space-fill="false">
          <variable>varCORNER_RADIUS</variable>
          <default>'${defCORNER_RADIUS}'</default>
        </spinbutton>
      </hbox>

      <text space-expand="true" space-fill="true"><label>""</label></text>
      <hseparator></hseparator>
      
      <hbox>
        <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$(gettext "Snap")'</b>"</label></text>
        <radiobutton>
          <variable>varSNAP_NONE</variable>
          <default>'${defSNAP_NONE}'</default>
          <label>'$(gettext "None")'</label>
        </radiobutton>
        <radiobutton>
          <variable>varSNAP_SCREEN</variable>
          <default>'${defSNAP_SCREEN}'</default>
          <label>'$(gettext "Screen")'</label>
        </radiobutton>
        <radiobutton>
          <variable>varSNAP_BORDER</variable>
          <default>'${defSNAP_BORDER}'</default>
          <label>'$(gettext "Border")'</label>
        </radiobutton>
      </hbox>

      <hbox tooltip-text="'$(gettext 'SLOPPY FOCUS
The window that is under the mouse pointer is the window that has the focus. The focus is changed by pointing at another window, there is no need to click in it as well.

CLICK TO FOCUS
The model used by Microsoft Windows. The active window is selected by mouse click.')'">
        <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$(gettext "Focus")'</b>"</label></text>
        <radiobutton>
          <variable>varFOCUS_CLICK</variable>
          <default>'${defFOCUS_CLICK}'</default>
          <label>'$(gettext "Click")'</label>
        </radiobutton>
        <radiobutton space-fill="false" space-expand="false">
          <variable>varFOCUS_SLOPPY</variable>
          <default>'${defFOCUS_SLOPPY}'</default>
          <label>'$(gettext "Sloppy")'</label>
        </radiobutton>
      </hbox>

      <text space-expand="true" space-fill="true"><label>""</label></text>

      <vbox>
        <hbox tooltip-text="'$(gettext 'How should the window be shown when moving')'">
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$(gettext "Move")'</b>"</label></text>
          <radiobutton>
            <variable>varMOVE_OUTLINE</variable>
            <default>'${defMOVE_OUTLINE}'</default>
            <label>'$(gettext "Outline")'</label>
          </radiobutton>
          <radiobutton>
            <variable>varMOVE_OPAQUE</variable>
            <default>'${defMOVE_OPAQUE}'</default>
            <label>'$(gettext "Opaque")'</label>
          </radiobutton>
        </hbox>
        <hbox tooltip-text="'$(gettext 'How should the window be shown when resizing')'">
          <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"<b>'$(gettext "Resize")'</b>"</label></text>
          <radiobutton>
            <variable>varRESIZE_OUTLINE</variable>
            <default>'${defRESIZE_OUTLINE}'</default>
            <label>'$(gettext "Outline")'</label>
          </radiobutton>
          <radiobutton>
            <variable>varRESIZE_OPAQUE</variable>
            <default>'${defRESIZE_OPAQUE}'</default>
            <label>'$(gettext "Opaque")'</label>
          </radiobutton>
        </hbox>
 
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <hseparator></hseparator>

        <checkbox tooltip-text="'$(gettext 'Drag the window to one of the desktop borders. Atfer a delay of 1 sec, it will jump over to the neighbor desktop')'">
          <variable>varMOVE_DELAY</variable>
          <default>'${defMOVE_DELAY}'</default>
          <label>'$(gettext "Allow moving window to neighbor desktop")'</label>
        </checkbox>
        <checkbox tooltip-text="'$(gettext 'Dragging the window to the top, it will maximize, but to the sides, it will fit to half the screen. An easy way to make 2 apps fit the whole desktop')'">
          <variable>varAEROSNAP</variable>
          <default>'${defAEROSNAP}'</default>
          <label>'$(gettext "Auto-maximize when moving window to top/side of desktop")'</label>
        </checkbox>
        <checkbox>
          <variable>varTILED</variable>
          <default>'${defTILED}'</default>
          <label>'$(gettext "Tile windows - place new window on empty space")'</label>
        </checkbox>
        <checkbox>
          <variable>varNOTURGENT</variable>
          <default>'${defNOTURGENT}'</default>
          <label>'$(gettext "Flash urgent windows in tray")'</label>
        </checkbox>
      </vbox>
    </frame>
  </vbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon windows_theme.svg`"'
      <action>'$APPDIR'/theme_switcher &</action>
      <label>" '$(gettext 'Color theme')' "</label>
    </button>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon windows_buttons.svg`"'
      <action>'$APPDIR'/window_buttons &</action>
      <label>" '$(gettext 'Buttons theme')' "</label>
    </button>
    <text space-expand="true" space-fill="true"><label>"   "</label></text>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
      <label>'$(gettext "Quit")'</label>
      <action>exit:EXIT</action>
    </button>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
      <label>'$(gettext "Apply")'</label>
      <action>func_save_settings</action>
    </button>
  </hbox>
</vbox>
</window>'

#------------------------------------------------------------------------------

[ -e "$CONFIG_FILE" ] || { xmessage -center -bg red -title "$(gettext "Error")" "$(gettext "Configuration file doesn't exist! Aborting...")"; exit 1; }
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -p GUI_MAIN
